<template>
  <div>
    <a-modal
      v-model:open="modalOpen"
      title="发放优惠券"
      width="50%"
      :footer="null"
      @cancel="handleCloseDialog"
    >
      <a-space direction="vertical" style="width: 100%" :size="16">
        <a-form
          :model="formData"
          ref="formRef"
          labelAlign="left"
          :label-col="{ span: 3 }"
          :wrapper-col="{ span: 21 }"
        >
          <a-form-item
            label="用户账号"
            name="username"
            :rules="[{ required: true, message: '输入用户账号' }]"
          >
            <a-textarea
              v-model:value="formData.username"
              placeholder="输入用户账号"
            />
          </a-form-item>
          <a-form-item
            label="发放数量"
            name="num"
            :rules="[{ required: true, message: '输入发放数量' }]"
          >
            <a-input
              v-model:value="formData.num"
              type="number"
              placeholder="输入发放数量"
            />
          </a-form-item>
          <a-form-item :wrapper-col="{ offset: 9, span: 4 }">
            <div class="btns center">
              <a-space>
                <a-button @click="handleCloseDialog">取消</a-button>
                <a-button type="primary" @click="handleSubmit">确定</a-button>
              </a-space>
            </div>
          </a-form-item>
        </a-form>
      </a-space>
    </a-modal>
  </div>
</template>
<script setup>
import { ref } from "vue";
import { grantUserCoupon } from "@/api/market";
const modalOpen = ref(false);
const formData = ref({
  couponId: undefined,
  username: undefined,
  num: undefined,
});
const formRef = ref(null);
const open = (id) => {
  modalOpen.value = true;
  formData.value.couponId = id;
};
function handleCloseDialog() {
  modalOpen.value = false;
  formData.value = {
    couponId: undefined,
    username: undefined,
    num: undefined,
  };
  formRef.value.resetFields();
}

const emits = defineEmits(["refreshList"]);
function handleSubmit() {
  formRef.value.validate().then(() => {
    grantUserCoupon(formData.value).then((res) => {
      if (res.code === 200) {
        handleCloseDialog();
        emits("refreshList");
      }
    });
  });
}
defineExpose({ modalOpen, open });
</script>
